<template>
    <div class="delivery-list">
        <div class="delivery-list-card">
            <div class="delivery-list-head">
                <Row :gutter="5">
                    <Col span="4">
                        <Input clearable v-model="searchData.name" placeholder="快递名" type="text"></Input>
                    </Col>
                    <Col span="4">
                        <Input clearable v-model="searchData.number" placeholder="快递编号" type="text"></Input>
                    </Col>
                    <Col span="16">
                        <ButtonGroup class="subSearch">
                            <Button :loading="search_loading" @click="searchGetList" type="primary"><Icon type="ios-pulse-strong"></Icon> 筛 选</Button>
                            <Button type="primary" @click="add"><Icon type="plus-round"></Icon> 新增快递</Button>
                        </ButtonGroup>
                    </Col>
                </Row>
            </div>
            <i-table height="680" stripe :columns="columns" :data="data"></i-table>
            <div style="padding: 15px;">
                <Page @on-page-size-change="changeTiao" @on-change="changePage" :current="index" :total="total" show-elevator show-sizer :placement="'top'" :page-size="page_con" :page-size-opts="[10, 15, 20, 30, 40, 50]"></Page>
            </div>
        </div>
        <Modal v-model="modal2" width="360">
            <p slot="header" style="text-align:center">
                <a>
                    <Icon type="information-circled"></Icon>
                    <span>删除确认</span>
                </a>
            </p>
            <div style="text-align:center">
                <p>是否继续删除？</p>
            </div>
            <div slot="footer">
                <Button type="primary" size="large" long :loading="modal_loading" @click="del">删除</Button>
            </div>
        </Modal>
        <!--新增快递-->
        <Modal v-model="modal3" width="450">
            <p slot="header" style="text-align:center">
                <a><Icon type="plus-round"></Icon> 新增快递</a>
            </p>
            <Form style="margin-top: 20px" ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                <FormItem label="快递名" prop="name">
                    <Input v-model="formValidate.name" placeholder="请输入快递名"></Input>
                </FormItem>
                <FormItem label="快递编号:"  prop="number">
                    <Input v-model="formValidate.number" placeholder="请输入快递编号"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" size="large" long :loading="modal_loading" @click="addDelivery">确定</Button>
            </div>
        </Modal>
        <!--修改快递-->
        <Modal v-model="modal4" width="450">
            <p slot="header" style="text-align:center">
                <a><Icon type="plus-round"></Icon> 修改快递</a>
            </p>
            <Form style="margin-top: 20px" ref="formValidateChange" :model="formValidateChange" :rules="ruleValidateChange" :label-width="80">
                <FormItem label="快递名" prop="name">
                    <Input v-model="formValidateChange.name" placeholder="请输入快递名"></Input>
                </FormItem>
                <FormItem label="快递编号:"  prop="number">
                    <Input v-model="formValidateChange.number" placeholder="请输入快递编号"></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="primary" size="large" long :loading="modal_loading" @click="changeDelivery">确定</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
    export default {
        name:"delivery-list",
        data(){
            return{
                columns:[
                    {
                        title: '序号',
                        type: 'index',
                        width: 60,
                        align:"center",
                    },
                    {
                        title: '快递名称',
                        key: 'name',
                        align:"center",
                    },
                    {
                        title: '快递编号',
                        key: 'number',
                        align:"center",
                    },
                    {
                        title: '操作',
                        key: 'action',
                        align: 'center',
                        render: (h, params) => {
                            return h('ButtonGroup', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                    },
                                    on: {
                                        click: () => {
                                            this.formValidateChange.name=params.row.name
                                            this.formValidateChange.number=params.row.number
                                            this.id = params.row.kuaidi_id
                                            this.modal4 = true
                                        }
                                    }
                                }, '修改'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                    },
                                    on: {
                                        click: () => {
                                            this.id = params.row.kuaidi_id
                                            this.modal2 = true
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    },
                ],
                data:[],
                total:0,
                index:1,
                page_con:15,
                modal2:false,
                id:"",//操作的下标
                modal_loading:false,
                searchData:{
                    name:"",//快递名
                    number:"",//快递编号
                },
                search_loading:false,
                modal3:false,
                formValidate:{
                    name:"",//快递名
                    number:"",//快递编号
                },
                ruleValidate:{
                    name: [
                        { required: true, message: '请输入快递名', trigger: 'blur' },
                    ],
                    number: [
                        { required: true, message: '请输入快递编号', trigger: 'blur' },
                    ]
                },
                modal4:false,
                formValidateChange:{
                    name:"",//快递名
                    number:"",//快递编号
                },
                ruleValidateChange:{
                    name: [
                        { required: true, message: '请输入快递名', trigger: 'blur' },
                    ],
                    number: [
                        { required: true, message: '请输入快递编号', trigger: 'blur' },
                    ]
                }
            }
        },
        activated(){
            this.getGoodsBrand()
        },
        methods:{
            getGoodsBrand(){
                this.getJsonData("Kuaidi/getAllKuaidi",{
                    num:this.page_con,
                    page:this.index,
                    name:this.searchData.name,
                    number:this.searchData.number,
                },true)
                    .then((res)=>{
                        this.data=res.data
                        this.total=parseInt(res.count)
                        this.search_loading=false
                    })
                    .catch((res)=>{
                        this.search_loading=false
                    })
            },
            changePage(n){
                this.index=n
                this.getGoodsBrand()
            },
            changeTiao(num){
                this.page_con=num
                this.index=1
                this.getGoodsBrand()
            },
            searchGetList(){
                this.search_loading=true
                this.index=1
                this.getGoodsBrand()
            },
            del(){
                this.modal_loading = true;
                this.getJsonData("Kuaidi/deleteKuaidi",{kuaidi_id:this.id},true)
                    .then((res)=>{
                        this.getGoodsBrand()
                        this.modal_loading = false;
                        this.modal2 = false;
                        this.$Message.success('删除成功');
                    }).catch((res)=>{
                    this.modal_loading = false;
                    this.modal2 = false;
                })
            },
            add(){
                this.formValidate.name=""
                this.formValidate.number=""
                this.modal3 = true
            },
            addDelivery(){
                this.$refs.formValidate.validate((valid) => {
                    if (valid) {
                        this.sureAddDelivery()
                    }
                })
            },
            sureAddDelivery(){
                this.modal_loading = true;
                this.getJsonData("Kuaidi/addKuaidi",{
                    name:this.formValidate.name,
                    number:this.formValidate.number,
                },true)
                    .then((res)=>{
                        this.getGoodsBrand()
                        this.modal_loading = false;
                        this.modal3 = false;
                        this.$Message.success('新增成功');
                    }).catch((res)=>{
                    this.modal_loading = false;
                    this.modal3 = false;
                })
            },
            changeDelivery(){
                this.$refs.formValidateChange.validate((valid) => {
                    if (valid) {
                        this.sureChangeDelivery()
                    }
                })
            },
            sureChangeDelivery(){
                this.modal_loading = true;
                this.getJsonData("Kuaidi/updateKuaidi",{
                    name:this.formValidateChange.name,
                    number:this.formValidateChange.number,
                    kuaidi_id:this.id,
                },true)
                    .then((res)=>{
                        this.getGoodsBrand()
                        this.modal_loading = false;
                        this.modal4 = false;
                        this.$Message.success('修改成功');
                    }).catch((res)=>{
                    this.modal_loading = false;
                    this.modal4 = false;
                })
            }
        },
    }
</script>
<style lang="less">
    @import "delivery-list";
</style>